/*
 * @Author: xkcyy xkcyy@outlook.com
 * @Date: 2025-05-06 13:03:40
 * @LastEditors: xkcyy xkcyy@outlook.com
 * @LastEditTime: 2025-05-06 13:06:53
 * @FilePath: \backend-java\src\test\java\com\xkcyy\one\agents\trainable\OpenAIToolAgentTest copy.java
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package com.xkcyy.one.ai.trainable;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xkcyy.one.ai.agents.CallableToolAgent;
import com.xkcyy.one.ai.agents.OpenAIToolAgent;
import com.xkcyy.one.ai.tools.ListDirectoryTool;
import com.xkcyy.one.ai.llm.OpenAiClient;
import com.xkcyy.one.ai.llm.model.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import reactor.core.publisher.Flux;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * OpenAIToolAgent集成测试
 * 
 * @author yuand
 */
@SpringBootTest
@ActiveProfiles("test")
class OpenAIToolAgentTest2 {


        @Test
        void testChatStreamWithToolCalls() throws InterruptedException {
            CallableToolAgent openAIToolAgent = new CallableToolAgent(new OpenAiClient());
            ListDirectoryTool tool = new ListDirectoryTool();
            openAIToolAgent.registerTool(tool);

            CountDownLatch countDownLatch = new CountDownLatch(1);
                // 准备测试数据
                String toolPrompt = "目录中D:\\04_ai\\system-prompts-and-models-of-ai-tools 有哪些markdown文件";
                List<ChatMessage> messages = Collections.singletonList(
                                ChatMessage.builder().role("user").content(toolPrompt).build());

                // 执行测试
                Flux<ChatResult> result = openAIToolAgent.chatStream(messages);

                // 添加打印逻辑和错误处理
                result.doOnNext(chatResult -> {
                    System.out.println("内容: " + StrUtil.emptyToDefault(chatResult.getContent(), chatResult.getReasoningContent()));
                    try {
                        if(CollectionUtil.isNotEmpty(chatResult.getToolCalls())){
                            System.out.println("工具调用: " + (new ObjectMapper()).writeValueAsString(chatResult.getToolCalls()));
                        }
                    } catch (JsonProcessingException e) {
                        throw new RuntimeException(e);
                    }
                })
                .doOnError(error -> {
                    System.err.println("发生错误: " + error.getMessage());
                    error.printStackTrace();
                })
                .doOnComplete(() -> {
                    System.out.println("流处理完成");
                })
                        .doFinally(x->countDownLatch.countDown())
                .subscribe();

                // 等待一段时间让异步操作完成
                countDownLatch.await();
        }
}